package gui;

import gui.util.StandardForm;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

import core.Transaction;
import core.TransactionDirection;

public class MainWindow extends JFrame
{
	private Gui gui;
	private TransactionsTable incomeList;
	private TransactionsTable expenseList;
		
	public MainWindow(String name, Gui gui){
		super(name);
		this.gui = gui;
		setSize(new Dimension(800, 800));
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
		StandardForm contentPane = new StandardForm(prepareTables(), new JButton("Zavřít"));
		
		setContentPane(contentPane.getContainer());
		setJMenuBar(prepareMenuBar());
	}

	public MainWindow(Gui gui){
		this("", gui);
	}

	public Component prepareTables(){
		Dimension minSize = new Dimension(100, 0);
		
		incomeList = new TransactionsTable(Color.GREEN, "Příjmy");
		incomeList.addMouseListener(new EditTransactionListener(incomeList));
		incomeList.addKeyListener(new DeleteTransactionListener(incomeList));
		JPanel incomePanel = new JPanel(new BorderLayout(0, 5));
		JScrollPane incomeSrollPane = new JScrollPane(incomeList);
		incomePanel.add(new JLabel("Příjmy"), BorderLayout.NORTH);
		incomePanel.add(incomeSrollPane);
		incomePanel.setMinimumSize(minSize);
		
		expenseList = new TransactionsTable(Color.RED, "Výdaje");
		expenseList.addMouseListener(new EditTransactionListener(expenseList));
		expenseList.addKeyListener(new DeleteTransactionListener(expenseList));
		JPanel spendingPanel = new JPanel(new BorderLayout(0, 5));
		JScrollPane spendingSrollPane = new JScrollPane(expenseList);
		spendingPanel.add(new JLabel("Výdaje"), BorderLayout.NORTH);
		spendingPanel.add(spendingSrollPane);
		spendingPanel.setMinimumSize(minSize);
		
		JSplitPane panel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, 
					incomePanel, spendingPanel);
		
		panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
		panel.setResizeWeight(0.5);
		
		return panel;
	}
	
	private JMenuBar prepareMenuBar(){
		JMenuBar bar = new JMenuBar();
			JMenu transactionsMenu = new JMenu("Transakce");
				JMenuItem newTransactionMenuItem = new JMenuItem("Nová transakce");
				newTransactionMenuItem.addActionListener(new NewTransactionListener());
			transactionsMenu.add(newTransactionMenuItem);
		bar.add(transactionsMenu);
		
		return bar;
	}

	public TransactionsTable getIncomeList(){
		return incomeList;
	}

	public TransactionsTable getExpenseList(){
		return expenseList;
	}
	
	private class NewTransactionListener implements ActionListener
	{
		@Override public void actionPerformed(ActionEvent ae){
			Transaction transaction = TransactionDialogs.newTransaction(MainWindow.this, gui.getProfile());
			
			if(transaction != null){
				gui.getProfile().getData().addTransaction(transaction);
				if(transaction.getDirection() == TransactionDirection.INCOME){
					incomeList.addTransaction(transaction);
				}
				else{
					expenseList.addTransaction(transaction);
				}
			}
		}
	}	
	
	private class EditTransactionListener extends MouseAdapter
	{
		private TransactionsTable table;
		
		private EditTransactionListener(TransactionsTable table){
			this.table = table;
		}
		
		@Override public void mouseClicked(MouseEvent e){
			if(e.getClickCount() >= 2){
				Transaction transaction = table.getSelectedTransaction();
				if(transaction != null){
					String oldType = transaction.getType();
					boolean result = TransactionDialogs.editTransaction(transaction,
											MainWindow.this, gui.getProfile());
					if(result){
						gui.getProfile().getData().transactionEdited(transaction);
						table.editTransaction(transaction, oldType);
					}
				}
			}
		}
	}
	
	private class DeleteTransactionListener extends KeyAdapter
	{
		private TransactionsTable table;
		
		private DeleteTransactionListener(TransactionsTable table){
			this.table = table;
		}
		
		@Override public void keyReleased(KeyEvent e){
			if(e.getKeyCode() == KeyEvent.VK_DELETE){
				Transaction transaction = table.getSelectedTransaction();
				if(transaction != null){
					String oldType = transaction.getType();
					boolean result = TransactionDialogs.deleteTransaction(transaction, MainWindow.this);
					if(result){
						gui.getProfile().getData().deleteTransaction(transaction);
						table.deleteTransaction(transaction);
					}
				}
			}
		}
	}
}
